<?php header("Content-Type:text/html;   charset=utf-8"); ?>
<script type="text/javascript">
var curtypeUserImg = "1";
$(function(){
    loadUserImg();
    bindSubmitUserImg();

    $("#imgSH").bind("click",function(){
        curtypeUserImg = "1";
        $("#fileuserimg").trigger("click");
    });
    $("#imgMT").bind("click",function(){
        curtypeUserImg = "2";
        $("#fileuserimg").trigger("click");
    });
    $("#imgSY").bind("click",function(){
        curtypeUserImg = "3";
        $("#fileuserimg").trigger("click");
    });
    $("#imgHY").bind("click",function(){
        curtypeUserImg = "4";
        $("#fileuserimg").trigger("click");
    });
    $("#imgRZ").bind("click",function(){
        curtypeUserImg = "5";
        $("#fileuserimg").trigger("click");
    });
    $("#fileuserimg").bind("change",loadUserImgImg);

    $("#btnDelSH").bind("click",function(){
        curtypeUserImg="1";
        saveUserImg(curtypeUserImg,"");
        loadUserImg();
    });
    $("#btnDelMT").bind("click",function(){
        curtypeUserImg="2";
        saveUserImg(curtypeUserImg,"");
        loadUserImg();
    });
    $("#btnDelSY").bind("click",function(){
        curtypeUserImg="3";
        saveUserImg(curtypeUserImg,"");
        loadUserImg();
    });
    $("#btnDelHY").bind("click",function(){
        curtypeUserImg="4";
        saveUserImg(curtypeUserImg,"");
        loadUserImg();
    });
    $("#btnDelRZ").bind("click",function(){
        curtypeUserImg="5";
        saveUserImg(curtypeUserImg,"");
        loadUserImg();
    });
    $("#btnSubmitUserImg").bind("click",submitUserImg);

});

function bindSubmitUserImg(){
    var options = {
        beforeSubmit:  showRequestUserImg,  //提交前处理
        success:       showResponseUserImg,  //处理完成
        resetForm: false,
        dataType:  'json'
    };

    $('#formUserImg').submit(function() {
        $(this).ajaxSubmit(options);
        return false;
    });
}
function showRequestUserImg(formData, jqForm, options) {
    return true;
}


function showResponseUserImg(data, statusText)  {
    if(data.ReturnCode==1){
        if(curtypeUserImg=="1"){
            $("#imgSH").attr("src",data.src);
        }else if(curtypeUserImg=="2"){
            $("#imgMT").attr("src",data.src);
        }else if(curtypeUserImg=="3"){
            $("#imgSY").attr("src",data.src);
        }else if(curtypeUserImg=="4"){
            $("#imgHY").attr("src",data.src);
        }else if(curtypeUserImg=="5"){
            $("#imgRZ").attr("src",data.src);
        }
        saveUserImg(curtypeUserImg,data.src);
    }else{

    }
    return false;
}

function saveUserImg(type,src){
    var params = {
        type:type,
        url:src
    };

    $.ajax({
        type:"post",
        url:"admin.php/manage/saveUserImg",
        dataType:"json",
        data:params,
        async:false,
        success:function(data){

        }
    });
}

function submitUserImg(){
    if(checkUserImg("txtSH",25)==false)
        return false;
    if(checkUserImg("txtMT",25)==false)
        return false;
    if(checkUserImg("txtSY",25)==false)
        return false;
    if(checkUserImg("txtHY",25)==false)
        return false;
    if(checkUserImg("txtRZ",25)==false)
        return false;

    var shisvalid = $("#selSH").val();
    var sh = $("#txtSH").val();

    var mtisvalid = $("#selMT").val();
    var mt = $("#txtMT").val();

    var syisvalid = $("#selSY").val();
    var sy = $("#txtSY").val();

    var hyisvalid = $("#selHY").val();
    var hy = $("#txtHY").val();

    var rzisvalid = $("#selRZ").val();
    var rz = $("#txtRZ").val();


    var params = {
        shisvalid:shisvalid,
        sh:sh,
        mtisvalid:mtisvalid,
        mt:mt,
        syisvalid:syisvalid,
        sy:sy,
        hyisvalid:hyisvalid,
        hy:hy,
        rzisvalid:rzisvalid,
        rz:rz
    };


    $.ajax({
        type:"post",
        url:"admin.php/manage/setUserImg",
        dataType:"json",
        data:params,
        success:function(data){
            loadUserImg();
            alert("提交成功");
        }
    });
}

function loadUserImg(){
    $.ajax({
        type:"post",
        url:"admin.php/manage/getUserImg",
        dataType:"json",
        success:function(data){
            $("#selSH").val(data.shisvalid);
            $("#txtSH").val(data.sh);
            $("#imgSH").attr("src",data.shurl);
            $("#selMT").val(data.mtisvalid);
            $("#txtMT").val(data.mt);
            $("#imgMT").attr("src",data.mturl);
            $("#selSY").val(data.syisvalid);
            $("#txtSY").val(data.sy);
            $("#imgSY").attr("src",data.syurl);
            $("#selHY").val(data.hyisvalid);
            $("#txtHY").val(data.hy);
            $("#imgHY").attr("src",data.hyurl);
            $("#selRZ").val(data.rzisvalid);
            $("#txtRZ").val(data.rz);
            $("#imgRZ").attr("src",data.rzurl);
        }
    });
}

function loadUserImgImg(){
    var fileName = $("#fileuserimg").val();
    var ext=fileName.substr(fileName.indexOf(".")).toLowerCase();
    if(ext!=".gif" && ext!=".jpg" && ext!=".jpeg" && ext!=".png" && ext!=""){
        alert("图片格式无效");
        return;
    }
    if(!IsEmpty(fileName)){
        $("#formUserImg").submit();
        return false;
    }
}

function checkUserImg(id,len){
    var name = $("#"+id).val();
    var length = name.length;
    if(length>len){
        SetError(id,"最多"+len+"个字符");
        return false;
    }
    ClearError(id);
    return true;
}

</script>
<div class="pageContent">
    <form id="formUserImg" class="pageForm required-validate"
          action="admin.php/manage/uploadImg/mypicuserimg" method="post"
          enctype="multipart/form-data">
        <div class="pageFormContent" style="width: 98%;" layoutH="56">
            <input type='file' id='fileuserimg' name='mypicuserimg'
                   style='display: none'>

            <table class="tableNewUser">


                <tr>
                    <td>商户图标：</td>
                    <td><select id="selSH">
                            <option value="1">开启</option>
                            <option value="0">关闭</option>
                        </select></td>
                    <td
                        style="vertical-align: middle; line-height: 130px; clear: both; width: 260px;"><input
                            type="text" id="txtSH" style="clear: both"></td>
                    <td style="text-align: left"><img id="imgSH" style="width: 60px; height: 60px; vertical-align: middle; cursor: pointer" src="uploads\20140523/537f70df3fac9.jpgs">
                    </td>
                </tr>
                <tr>
                    <td>模特图标：</td>
                    <td><select id="selMT">
                            <option value="1">开启</option>
                            <option value="0">关闭</option>
                        </select></td>
                    <td><input type="text" id="txtMT" style="clear: both"></td>
                    <td><img id="imgMT"
                             style="width: 60px; height: 60px; vertical-align: middle; cursor: pointer">
                    </td>
                </tr>
                <tr>
                    <td>摄影图标：</td>
                    <td><select id="selSY">
                            <option value="1">开启</option>
                            <option value="0">关闭</option>
                        </select></td>
                    <td><input type="text" id="txtSY" style="clear: both"></td>
                    <td><img id="imgSY"
                             style="width: 60px; height: 60px; vertical-align: middle; cursor: pointer">
                    </td>
                </tr>
                <tr>
                    <td>会员图标：</td>
                    <td><select id="selHY">
                            <option value="1">开启</option>
                            <option value="0">关闭</option>
                        </select></td>
                    <td><input type="text" id="txtHY" style="clear: both"></td>
                    <td><img id="imgHY"
                             style="width: 60px; height: 60px; vertical-align: middle; cursor: pointer">
                    </td>
                </tr>
                <tr>
                    <td>认证图标：</td>
                    <td><select id="selRZ">
                            <option value="1">开启</option>
                            <option value="0">关闭</option>
                        </select></td>
                    <td><input type="text" id="txtRZ" style="clear: both"></td>
                    <td><img id="imgRZ"
                             style="width: 60px; height: 60px; vertical-align: middle; cursor: pointer">
                    </td>
                </tr>
                <tr>
                    <td colspan="4" style='line-height: 40px'>&nbsp;&nbsp;</td>
                </tr>
            </table>


            <div class="buttonActive" style="width: 120px; margin-left: 50px;">
                <div class="buttonContent">
                    <button id="btnSubmitUserImg" type="button" style="width: 80px">提 交</button>
                </div>
            </div>

            <div class="button" style="text-align: right">
                <div class="buttonContent" style="text-align: right">
                    <button id="btnCloseUserImg" type="button" class="close"
                            style="width: 80px">关 闭</button>
                </div>
            </div>
        </div>
    </form>
</div>
